package com.adtalos.flutter.msa.sdk;

import android.app.Application;
import android.content.Context;

import androidx.annotation.NonNull;

import com.github.gzuliyujiang.oaid.DeviceID;
import com.github.gzuliyujiang.oaid.DeviceIdentifier;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;

/**
 * FlutterMsaSdkPlugin
 */
public class FlutterMsaSdkPlugin implements FlutterPlugin , MethodCallHandler {
    private final static FlutterMsaSdkPlugin plugin = new FlutterMsaSdkPlugin();
    private Context context;
    private MethodChannel channel;
    private FlutterPluginBinding pluginBinding;

    @Override
    public void onAttachedToEngine(FlutterPluginBinding binding) {
        pluginBinding = binding;
        initializePlugin(binding.getApplicationContext(), binding.getBinaryMessenger());
    }

    @Override
    public void onDetachedFromEngine(FlutterPluginBinding binding) {
        pluginBinding = null;
    }

    private void initializePlugin(Context context, BinaryMessenger messenger) {
        this.channel = new MethodChannel(messenger, "flutter_msa_sdk");
        channel.setMethodCallHandler(this);
        this.context = context;
    }

    @Override
    public void onMethodCall(@NonNull MethodCall call, @NonNull final Result result) {
        switch (call.method) {
            case "isSupport":
                result.success(DeviceID.supportedOAID(context));
                return;
            case "register":
                DeviceIdentifier.register((Application) pluginBinding.getApplicationContext());
                result.success(true);
                return;
            case "getIMEI":
                result.success(DeviceIdentifier.getIMEI(context));
                return;
            case "getOAID":
                result.success(DeviceIdentifier.getOAID(context));
                return;
            default:
                result.notImplemented();
        }
    }
}
